package jgespres;

import java.awt.*;
import java.util.*;
import java.text.*;
import javax.swing.*;
import javax.swing.text.*;
import java.awt.event.*;

/**
 * Editeur de théme de présentation
 * 
 * @author guerfyld
 * @version 2.0
 */
public class JThemeEditor extends JDialog {
    // Elements de l'editeur
    private Theme Thm;
    private boolean changed;
    private boolean nextPressed;
    
    // Elements de l'interface
    private JTextField fldIdent;
    private JFormattedTextField fldDate;
    private JTextField fldLabel;
    private JTextField fldLargeLabel;
    
    // Date format
    private DateFormat df;
    
    public JThemeEditor() {
        // Appel constructeur (Boite de dialogue Modal)
        super(JFrame.getFrames()[0], jgespres.getResourceString("Theme.EditorTitle"), true);

        // Initialisation de l'objet
        Thm         = null;
        changed     = false;
        nextPressed = false;
        createGUI();
    }
    
    public JThemeEditor(Theme thm) {
        this();
        
        // Affectation du thème
        setEditor(thm);
    }
    
    private void createGUI() {
        // Définitions de la boite de dialogue
        setResizable(false);
        //setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
        getContentPane().setLayout( new BoxLayout(getContentPane(), BoxLayout.Y_AXIS) );

        // Récupère format de date numérique local
        df = DateFormat.getDateInstance(DateFormat.SHORT);
        df.setLenient(false);

        // Définition 1ére ligne
        JPanel line = new JPanel( new FlowLayout(FlowLayout.LEFT) );
        // -> Ident
        JLabel label = new JLabel(jgespres.getResourceString("Theme.Col0")+" :");
        label.setPreferredSize( new Dimension(100, 25) );
        line.add(label);
        fldIdent = new JTextField();
        fldIdent.setPreferredSize( new Dimension(100, 25) );
        line.add(fldIdent);
        // -> Date
        label = new JLabel(jgespres.getResourceString("Theme.Col1")+" :");
        label.setHorizontalAlignment(SwingConstants.RIGHT);
        label.setPreferredSize( new Dimension(250, 25) );
        line.add(label);
        // Créé masque de saisie de la date
        String pattern = df.format( new Date(0) ).replaceAll("\\d", "#");
        try {
            fldDate = new JFormattedTextField( new MaskFormatter(pattern) );
        }
        catch(Exception e) {
            fldDate = new JFormattedTextField();
        }
        fldDate.setPreferredSize( new Dimension(75, 25) );        
        fldDate.setInputVerifier(new InputVerifier() {
            public boolean verify(JComponent input) {
                return validDate(((JFormattedTextField) input).getText());
            }
        });
        fldDate.addKeyListener(new java.awt.event.KeyAdapter() {  
            public void keyPressed(java.awt.event.KeyEvent e) {
                if (fldDate.getForeground() != Color.BLACK)
                    fldDate.setForeground(Color.BLACK);
            }
        });
        line.add(fldDate);
        getContentPane().add(line);

        // Définition 2éme ligne
        line = new JPanel( new FlowLayout(FlowLayout.LEFT) );
        // -> Label
        label = new JLabel(jgespres.getResourceString("Theme.Col2")+" :");
        label.setPreferredSize( new Dimension(100, 25) );
        line.add(label);
        fldLabel = new JTextField();
        fldLabel.setPreferredSize( new Dimension(200, 25) );
        line.add(fldLabel);
        getContentPane().add(line);

        // Définition 3éme ligne
        line = new JPanel( new FlowLayout(FlowLayout.LEFT) );
        // -> Large label
        label = new JLabel(jgespres.getResourceString("Theme.Col3")+" :");
        label.setPreferredSize( new Dimension(100, 25) );
        line.add(label);
        fldLargeLabel = new JTextField();
        fldLargeLabel.setPreferredSize( new Dimension(600, 25) );
        line.add(fldLargeLabel);
        getContentPane().add(line);
        
        // Définition 4éme ligne
        line = new JPanel( new FlowLayout(FlowLayout.RIGHT) );
        // -> Boutons d'action
        JButton button = new JButton(jgespres.getResourceString("Dialog.Ok"));
        button.setPreferredSize( new Dimension(90, 25) );
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                okPerformed();
            }
        });
        line.add(button);
        button = new JButton(jgespres.getResourceString("Dialog.Cancel"));
        button.setPreferredSize( new Dimension(90, 25) );
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                cancelPerformed();
            }
        });
        line.add(button);
        button = new JButton(jgespres.getResourceString("Theme.Next"));
        button.setPreferredSize( new Dimension(90, 25) );
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                nextPerformed();
            }
        });
        line.add(button);
        getContentPane().add(line);

        pack();
        jgespres.centerDialog(this);
    }
    
    /**
     * Affecte un théme à l'editeur
     * 
     * @param   thm Théme à affecter
     */
    public void setEditor(Theme Thm) {
        // Initialisation de l'objet
        this.Thm = Thm;
        changed = false;
        nextPressed = false;
        fldIdent.setText(Thm.getIdent());
        fldDate.setValue( df.format( Thm.getDate() ) );
        fldLabel.setText(Thm.getLabel());
        fldLargeLabel.setText(Thm.getLargeLabel());
    }
    
    /**
     * Renvoi si des modifications du théme ont eu lieu
     * 
     * @return  vrai/faux
     */
    public boolean isChanged() {
        return changed;
    }
    
    /**
     * Renvoi si bouton 'Next' appuyé
     * 
     * @return  vrai/faux
     */
    public boolean doNext() {
        return nextPressed;
    }
    
    private void updateTheme() {
        Thm.setIdent( fldIdent.getText() );
        try {
            Thm.setDate( df.parse( (String) fldDate.getValue() ) );
        }
        catch(Exception e) {}
        Thm.setLabel( fldLabel.getText() );
        Thm.setLargeLabel( fldLargeLabel.getText() );
    }
    
    private void okPerformed() {
        if ( validDate( (String) fldDate.getText() ) ) {
            updateTheme();
            changed = true;
            setVisible(false);
        }
        else {
            fldDate.requestFocusInWindow();
        }
    }

    private void cancelPerformed() {
        setVisible(false);
    }

    private void nextPerformed() {
        if ( validDate( (String) fldDate.getText() ) ) {
            updateTheme();
            changed = true;
            nextPressed = true;
            setVisible(false);
        }
        else {
            fldDate.requestFocusInWindow();
        }
    }
    
    private boolean validDate(String text) {
        try {
            if (df.parse(text) != null) {
                fldDate.commitEdit();
                return true;
            }
        }
        catch(Exception e) {}
        
        // Indique date en erreur !
        fldDate.setForeground(Color.RED);
        
        return false;
    }
}
